package com.strongwell.awe.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.strongwell.awe.model.WxTokenEntity;
import com.strongwell.awe.model.WxUser;
import com.strongwell.awe.service.WxTokenService;
import com.strongwell.awe.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Array;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class WxUserDaoImpl implements WxUserDao {
    @Autowired
    public WxTokenService wxTokenService;
    @Override
    public List<WxUser> getWxUserList() {
        String wxToken = wxTokenService.getToken().getToken();
        String userList = getList(wxToken);
        JSONObject jsonObject = JSON.parseObject(userList);
        JSONObject openids = (JSONObject) jsonObject.get("data");
        JSONArray openidArray = (JSONArray) openids.get("openid");
        List<WxUser> wxUserList = new ArrayList<WxUser>();
        if (openidArray.size() > 0) {
            String userInfo = null;

            for (int i = 0; i < openidArray.size(); i++) {
                String openidss = openidArray.getString(i);
                userInfo = getUserInfo(wxToken, openidss);
                JSONObject userObject = JSON.parseObject(userInfo);
                System.out.println(userInfo);
                Integer subscribe = (Integer) userObject.get("subscribe");
                String openid = (String) userObject.get("openid");
                String nickname = (String) userObject.get("nickname");
                Integer sex = (Integer) userObject.get("sex");
                String language = (String) userObject.get("language");
                String city = (String) userObject.get("city");
                String province = (String) userObject.get("province");
                String country = (String) userObject.get("country");
                String headimgurl = (String) userObject.get("headimgurl");
                Integer subscribe_time = (Integer) userObject.get("subscribe_time");
                String remark = (String) userObject.get("remark");
                Integer groupid = (Integer) userObject.get("groupid");
                JSONArray tagid_list = (JSONArray) userObject.get("tagid_list");
                String subscribe_scene = (String) userObject.get("subscribe_scene");
                Integer qr_scene = (Integer) userObject.get("qr_scene");
                String qr_scene_str = (String) userObject.get("qr_scene_str");

                WxUser wxUser = new WxUser();
                wxUser.setSubscribe(subscribe);
                wxUser.setOpenid(openid);
                wxUser.setNickname(nickname);
                wxUser.setSex(sex);
                wxUser.setLanguage(language);
                wxUser.setCity(city);
                wxUser.setProvince(province);
                wxUser.setCountry(country);
                wxUser.setHeadimgurl(headimgurl);
                SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String sdate=format2.format(subscribe_time);

                wxUser.setSubscribeTime(Timestamp.valueOf(sdate));
                wxUser.setRemark(remark);
                wxUser.setGroupid(groupid);
                wxUser.setTagid_list(new JSONArray[]{tagid_list});
                wxUser.setSubscribe_scene(subscribe_scene);
                wxUser.setQr_scene(qr_scene);
                wxUser.setQr_scene_str(qr_scene_str);
                wxUserList.add(wxUser);
            }

        }
        System.out.println(wxUserList);
        return wxUserList;
    }

    public static String getUserInfo(String wxToken,String openid){
        //这里为请求接口的url   +号后面的是token，这里就不做过多对token获取的方法解释
        String url="https://api.weixin.qq.com/cgi-bin/user/info?access_token="+wxToken+"&openid="+openid+"&lang=zh_CN";

        try {
            String rs = HttpUtils.sendGetWithoutParams(url);
            return rs;
        } catch (Exception e) {
            System.out.println("请求错误！");
            return "false";
        }
    }

    //获取用户列表，返回用户的openID，{"total":2,
    //"count":2,
    //"data":{
    //"openid":["OPENID1","OPENID2"]},
    //"next_openid":"NEXT_OPENID"
    //}
    public static String getList(String wxToken){
        //这里为请求接口的url   +号后面的是token，这里就不做过多对token获取的方法解释
        String url="https://api.weixin.qq.com/cgi-bin/user/get?access_token="+wxToken;

        try {
            String rs = HttpUtils.sendGetWithoutParams(url);
            return rs;
        } catch (Exception e) {
            System.out.println("请求错误！");
            return "false";
        }
    }
}
